﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;

public partial class Police_Question_Examination : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            List<SlectDepartment> selectList = new List<SlectDepartment>();
            ViewState["selectList"] = selectList;
            BindSelect();
        }

    }

    void BindSelect()
    {
        List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;

        dptDepartment.Items.Clear();
        foreach (SlectDepartment selected in selectList)
        {
            dptDepartment.Items.Add(new ListItem(selected.text, selected.value));
        }

    }

    private void Bind()
    {
        for (int i = 0; i < 59; i++)
        {
            if (i < 24)
            {
                dptHours.Items.Add(new ListItem(i.ToString(), i.ToString()));
                dptHours2.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            dptMine.Items.Add(new ListItem(i.ToString(), i.ToString()));
            dptMine2.Items.Add(new ListItem(i.ToString(), i.ToString()));

        }
        BindDepartmentoffer();
        BindDepartment();
    }

    private void BindDepartmentoffer()
    {
        DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
        DepartmentList.DataBind();
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (!DepartmentID.Substring(2, 3).Equals("000"))
        {
            DepartmentList.SelectedValue = DepartmentID.Substring(0, 4) + "000";
            this.DepartmentList.Enabled = false;
        }  
        PermissionCategoryList_SelectedIndexChanged(this, new EventArgs());
    }

    private void BindDepartment()
    {
        DepartmentDataContext pt = new DepartmentDataContext();

        //Soldier soldier = new Soldier();

        //List<string> departmentids = soldier.GetHaveSoldierDepartmentIDs("DepartmentID like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%' And IsDelete=0");

        //var result2 = from p in pt.Account_Departments where departmentids.Contains(p.DepartmentID) select p;
        string DepartmentID=Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        Question2DataContext db = new Question2DataContext();
        Associate2 sec = (from o in db.Associate2 where o.SID == new Guid(Request["ID"]) && o.DepartmentID.Substring(0, 6) == DepartmentID.Substring(0, 6) select o).FirstOrDefault();
        string strSql = string.Empty;
        if (sec != null)
        {
            int Flag = (from obj in db.Associate2 where obj.SID == new Guid(Request["ID"]) && obj.DepartmentID.Substring(0, 6) == DepartmentID.Substring(0, 6) select obj).FirstOrDefault().Flag.Value;
            
            //中队
            if (Flag == 0)
            {
                strSql = "SubString(DepartmentID,5,1)='3' And";
            }
            //大队
            if (Flag == 1)
            {
                strSql = "SubString(DepartmentID,5,1)='2' And";
            }
            //大中队
            if (Flag == 2)
            {
                strSql = "";
            }


            if (sec == null)
                return;

            this.txtStartTime.Text = sec.StartDate.Value.ToString("yyyy-MM-dd");
            this.txtEndTime.Text = sec.EndDate.Value.ToString("yyyy-MM-dd");
            dptHours.SelectedValue = sec.StartDate.Value.Hour.ToString();
            dptMine.SelectedValue = sec.StartDate.Value.Minute.ToString();

            dptHours2.SelectedValue = sec.EndDate.Value.Hour.ToString();
            dptMine2.SelectedValue = sec.EndDate.Value.Minute.ToString();

            txtEndTime.Enabled = false;
            txtStartTime.Enabled = false;
            dptMine2.Enabled = false;
            dptMine.Enabled = false;
            dptHours.Enabled = false;
            dptHours2.Enabled = false;
            txtEndTime_CalendarExtender.Enabled=txtStartTime_CalendarExtender.Enabled = false;



            
        }

        this.lstDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList(strSql+ " DepartmentID like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%'");
        this.lstDepartment.DataTextField = "FullName";
        this.lstDepartment.DataValueField = "DepartmentID";
        this.lstDepartment.DataBind();
    }

    protected void dptDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dptDepartment.SelectedValue.Equals("00"))
            return;

        string strWhere = string.Empty;
        if (dptDepartment.SelectedValue.Trim().Equals("11"))
        {
            strWhere = " 1=1 and IsDelete=0";
        }
        else
        {

            strWhere = "Left(DepartmentID,4)='" + dptDepartment.SelectedValue.Substring(0, 4) + "' and IsDelete=0 ";

        }

        Soldier soldier = new Soldier();

        List<string> departmentids = soldier.GetHaveSoldierDepartmentIDs(strWhere);
        List<Yipusoft.Account.Model.DepartmentInfo> departmentlist = Yipusoft.Account.BLL.Department.GetList("1=1 And Right(DepartmentID,3)<>'000'");
        var pp = from p in departmentlist where departmentids.Contains(p.DepartmentID) select p;
        List<Yipusoft.Account.Model.DepartmentInfo> list = pp.ToList();
        List<MyDepartmentInfo> myList = new List<MyDepartmentInfo>();
        foreach (Yipusoft.Account.Model.DepartmentInfo info in list)
        {
            myList.Add(new MyDepartmentInfo() { FullName = info.FullName, DepartmentID = info.DepartmentID });
        }
        ViewState["Department"] = myList;
        this.lstDepartment.DataSource = list;
        this.lstDepartment.DataTextField = "FullName";
        this.lstDepartment.DataValueField = "DepartmentID";
        this.lstDepartment.DataBind();


    }
    //protected void btnRemove_Click(object sender, EventArgs e)
    //{
    //    if (ViewState["Department"] == null)
    //        return;
    //    List<MyDepartmentInfo> list = ViewState["Department"] as List<MyDepartmentInfo>;

    //    foreach (ListItem item in lstDepartment.Items)
    //    {
    //        if (item.Selected)
    //        {
    //            MyDepartmentInfo info = list.Where(o => o.DepartmentID == item.Value).FirstOrDefault();
    //            list.Remove(info);
    //        }
    //    }


    //    ViewState["Department"] = list;
    //    this.lstDepartment.DataSource = list;
    //    this.lstDepartment.DataTextField = "FullName";
    //    this.lstDepartment.DataValueField = "DepartmentID";
    //    this.lstDepartment.DataBind();
    //}

    [Serializable]
    public class MyDepartmentInfo
    {
        public string FullName
        {
            set;
            get;
        }

        public string DepartmentID
        {
            set;
            get;
        }
    }
    protected void btnExamination_Click(object sender, EventArgs e)
    {

        if (ViewState["selectList"] == null)
            return;

        try
        {
            string userID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID;
            Question2DataContext db = new Question2DataContext();
            List<SlectDepartment> list = ViewState["selectList"] as List<SlectDepartment>;
            string[] strArry = Request["ID"].Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (SlectDepartment info in list)
            {
                foreach (string ID in strArry)
                {
                    Examination2 exam = new Examination2();
                    DateTime St = DateTime.Parse(txtStartTime.Text);
                    DateTime Et = DateTime.Parse(txtEndTime.Text);
                    St = St.AddHours(double.Parse(dptHours.SelectedValue));
                    St = St.AddMinutes(double.Parse(dptMine.SelectedValue));
                    Et = Et.AddHours(double.Parse(dptHours2.SelectedValue));
                    Et = Et.AddMinutes(double.Parse(dptMine2.SelectedValue));
                    exam.ID = Guid.NewGuid();
                    exam.SetUserID = userID;
                    exam.SID = new Guid(ID);
                    exam.StartTime = St;
                    exam.EndTime = Et;
                    exam.DepartmentID = info.value;
                    db.Examination2.InsertOnSubmit(exam);

                }
            }

            db.SubmitChanges();
            lblPrompt.Text = "操作已成功";
            this.btnExamination.Enabled = false;
        }
        catch (Exception ex)
        {
            lblPrompt.Text = ex.ToString();

        }

    }
    protected void PermissionCategoryList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDepartment();
    }
    protected void btnAddDepartment_Click(object sender, EventArgs e)
    {
        List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;
        foreach (ListItem Item in lstDepartment.Items)
        {
            if (Item.Selected)
            {
                int count = selectList.Where(or => or.value == Item.Value).Count();
                if (count == 0)
                {
                    selectList.Add(new SlectDepartment() { text = Item.Text, value = Item.Value });
                }
            }
        }
        ViewState["selectList"] = selectList;
        BindSelect();
    }
    protected void btnAddDepartmentAll_Click(object sender, EventArgs e)
    {
        List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;
        foreach (ListItem Item in lstDepartment.Items)
        {
            int count = selectList.Where(or => or.value == Item.Value).Count();
            if (count == 0)
            {
                selectList.Add(new SlectDepartment() { text = Item.Text, value = Item.Value });
            }
        }
        ViewState["selectList"] = selectList;
        BindSelect();
    }

    [Serializable]
    public class SlectDepartment
    {
        public string text;
        public string value;
    }
    protected void ButtonRemove_Click(object sender, EventArgs e)
    {
        List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;
        foreach (ListItem Item in dptDepartment.Items)
        {
            if (Item.Selected)
            {
                if (selectList.Where(or => or.value == Item.Value).Count() > 0)
                {
                    selectList.Remove(selectList.Where(or => or.value == Item.Value).Single());
                }
            }
        }
        ViewState["selectList"] = selectList;
        BindSelect();
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;
        selectList.Clear();
        ViewState["selectList"] = selectList;
        BindSelect();
    }
}
